//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.cgreport.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ht.modules.system.online.cgreport.entity.OnlCgreportItem;
import com.ht.modules.system.online.cgreport.service.IOnlCgreportItemService;
import com.ht.entity.Result;
import com.ht.query.QueryGenerator;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping({"/online/cgreport/item"})
public class OnlCgreportItemController {
    private static final Logger log = LoggerFactory.getLogger(OnlCgreportItemController.class);
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;

    public OnlCgreportItemController() {
    }

    @GetMapping({"/listByHeadId"})
    public Result<?> queryPageListByHeadId(String headId) {
        QueryWrapper<OnlCgreportItem> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cgrhead_id", headId);
        queryWrapper.orderByAsc(List.of("order_num"));
        List<OnlCgreportItem> list = this.onlCgreportItemService.list(queryWrapper);
        Result<List<OnlCgreportItem>> result = new Result<>();
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    @GetMapping({"/list"})
    public Result<IPage<OnlCgreportItem>> queryPageList(OnlCgreportItem onlCgreportItem, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<OnlCgreportItem>> result = new Result<>();
        QueryWrapper<OnlCgreportItem> queryWrapper = QueryGenerator.initQueryWrapper(onlCgreportItem, req.getParameterMap());
        Page<OnlCgreportItem> page = new Page((long)pageNo, (long)pageSize);
        IPage<OnlCgreportItem> pageList = this.onlCgreportItemService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @PostMapping({"/add"})
    public Result<OnlCgreportItem> add(@RequestBody OnlCgreportItem onlCgreportItem) {
        Result result = new Result<>();

        try {
            this.onlCgreportItemService.save(onlCgreportItem);
            result.success("添加成功！");
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            result.error500("操作失败");
        }

        return result;
    }

    @PutMapping({"/edit"})
    public Result<OnlCgreportItem> edit(@RequestBody OnlCgreportItem onlCgreportItem) {
        Result<OnlCgreportItem> result = new Result<>();
        OnlCgreportItem onlCgreportItemEntity = (OnlCgreportItem)this.onlCgreportItemService.getById(onlCgreportItem.getId());
        if (onlCgreportItemEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportItemService.updateById(onlCgreportItem);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/delete"})
    public Result<OnlCgreportItem> delete(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportItem> result = new Result<>();
        OnlCgreportItem onlCgreportItem = (OnlCgreportItem)this.onlCgreportItemService.getById(id);
        if (onlCgreportItem == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = this.onlCgreportItemService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }

        return result;
    }

    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgreportItem> deleteBatch(@RequestParam(name = "ids",required = true) String ids) {
        Result<OnlCgreportItem> result = new Result<>();
        if (ids != null && !"".equals(ids.trim())) {
            this.onlCgreportItemService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        } else {
            result.error500("参数不识别！");
        }

        return result;
    }

    @GetMapping({"/queryById"})
    public Result<OnlCgreportItem> queryById(@RequestParam(name = "id",required = true) String id) {
        Result<OnlCgreportItem> result = new Result<>();
        OnlCgreportItem onlCgreportItem = (OnlCgreportItem)this.onlCgreportItemService.getById(id);
        if (onlCgreportItem == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgreportItem);
            result.setSuccess(true);
        }

        return result;
    }
}
